# -*- mode: ruby; coding: utf-8 -*-

require 'English'

require 'find'
require 'fileutils'
require 'rubygems'
require 'hoe'

base_dir = File.join(File.dirname(__FILE__))
truncate_base_dir = Proc.new do |x|
  x.gsub(/^#{Regexp.escape(base_dir + File::SEPARATOR)}/, '')
end

lib_dir = File.join(base_dir, 'lib')
$LOAD_PATH.unshift(lib_dir)

require 'test-unit-ext'

Test::Unit.run = true

manifest = File.join(base_dir, "Manifest.txt")
manifest_contents = []
base_dir_included_components = %w(NEWS.en NEWS.ja README.en README.ja Rakefile)
excluded_components = %w(.svn .test-result pkg)
excluded_suffixes = %w(.png .ps .pdf .o .so .txt)
Find.find(base_dir) do |target|
  target = truncate_base_dir[target]
  components = target.split(File::SEPARATOR)
  if components.size == 1 and !File.directory?(target)
    next unless base_dir_included_components.include?(components[0])
  end
  Find.prune if (excluded_components - components) != excluded_components
  next if excluded_suffixes.include?(File.extname(target))
  manifest_contents << target if File.file?(target)
end

dummy_files = %w(History.txt README.txt)
FileUtils.touch("History.txt")
FileUtils.cp("README.en", "README.txt")

File.open(manifest, "w") do |f|
  f.puts manifest_contents.sort.join("\n")
end
at_exit do
  FileUtils.rm_f(manifest)
  FileUtils.rm_f(dummy_files)
end

def cleanup_white_space(entry)
  entry.gsub(/(\A\n+|\n+\z)/, '') + "\n"
end

version = TestUnitExt::VERSION
ENV["VERSION"] = version
project = Hoe.new('test-unit-ext', version) do |project|
  project.rubyforge_name = 'test-unit-ext'
  project.remote_rdoc_dir = "doc"
  project.author = ["Kouhei Sutou"]
  project.email = ['kou@cozmixng.org']
  project.summary = 'Ruby bindings for cairo'
  project.url = 'http://test-unit-ext.rubyforge.org/'

  news_of_current_release = File.read("NEWS.en").split(/^==\s.*$/)[1]
  project.changes = cleanup_white_space(news_of_current_release)

  entries = File.read("README.en").split(/^==\s(.*)$/)
  whats_this = cleanup_white_space(entries[entries.index("What\'s this?") + 1])
  project.summary, project.description, = whats_this.split(/\n\n+/, 3)
end

spec = project.spec
spec.rdoc_options = spec.rdoc_options.collect do |option|
  option == "README.txt" ? "README.en" : option
end
spec.extra_rdoc_files += spec.files.grep(/(:?README|NEWS)\.(:?ja|en)\z/)
spec.dependencies.delete_if {|dependency| dependency.name == "hoe"}

rdoc_task = nil
if ObjectSpace.each_object(Rake::RDocTask) {|rdoc_task|} != 1
  puts "hoe may be changed"
end
rdoc_task.main = "README.en"
rdoc_task.options.delete("-d")
rdoc_task.options << "--charset=UTF-8"
rdoc_task.template = "kilmer"
rdoc_task.rdoc_files += ["README.en", "NEWS.en"]

desc 'Tag the repository for release.'
task :tag do
  message = "Released TestUnitExt #{version}!"
  base = "svn+ssh://#{ENV['USER']}@rubyforge.org/var/svn/test-unit-ext/"
  sh 'svn', 'copy', '-m', message, "#{base}trunk", "#{base}tags/#{version}"
end

desc "Publish static HTML files"
task :publish_html do
  ruby "misc/rd2html.rb"
  Dir.glob("html/*.html.en") do |html_en|
    FileUtils.cp(html_en, html_en.sub(/\.en$/, ''))
  end
  config = YAML.load(File.read(File.expand_path("~/.rubyforge/user-config.yml")))
  host = "#{config["username"]}@rubyforge.org"
  remote_dir = "/var/www/gforge-projects/#{project.rubyforge_name}"
  local_dir = "html"
  sh "rsync #{project.rsync_args} #{local_dir}/*.html* #{host}:#{remote_dir}"
end
task :publish_docs => :publish_html

desc "Distribute new release."
task :dist => [:publish_docs, :release, :tag, :announce]
